import 'package:flutter/material.dart';
import 'package:mall/components/enter_store_button.dart';
import 'package:mall/components/image_network.dart';
import 'package:mall/core/models/store_index.dart';
import 'package:mall/screens/product/product_page.dart';
import 'package:mall/screens/store/store_screen.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../constants.dart';
import '../../../theme.dart';

/// 店铺
class HomeStore extends StatelessWidget {
  final List<Floor> store;

  const HomeStore({
    Key key,
    @required this.store,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 8.w,
      runSpacing: 8.w,
      children: List.generate(
        store.length,
        (index) => _buildFloorItem(context, store[index]),
      ),
    );
  }

  Widget _buildFloorItem(BuildContext context, Floor floor) {
    return GestureDetector(
      onTap: () => _onTapFloorItem(context, floor),
      child: Container(
        padding: EdgeInsets.all(4.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(4.w),
        ),
        width: (375 - 8 * 2).w,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                // logo
                ImageNetwork(
                  url: "${floor.linkStoreLogoUrl}",
                  width: 12.w,
                  height: 12.w,
                ),
                SizedBox(width: 4.w),
                // 名称
                Text(
                  "${floor.linkStoreName}",
                  style: TextStyle(fontSize: 13.sp),
                ),
                Spacer(),
                // 进店
                EnterStoreButton(),
              ],
            ),
            SizedBox(height: 4.w),
            Wrap(
              spacing: 4.w,
              runSpacing: 4.w,
              children: List.generate(
                floor.productLinks.length,
                (index) => _buildItem(context, floor.productLinks[index]),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 点击进店
  void _onTapFloorItem(BuildContext context, Floor floor) {
    Navigator.of(context).pushNamed(StoreScreen.routeName, arguments: floor.linkStoreId);
  }

  Widget _buildItem(BuildContext context, Link link) {
    return GestureDetector(
      onTap: () => _onTapItem(context, link),
      child: Container(
        width: ((375.0 - 8 * 2 - 4 * 5) ~/ 4).w,
        height: ((375.0 - 8 * 2 - 4 * 5) ~/ 4).w,
        decoration: BoxDecoration(
          border: Border.all(color: kLineColor),
          borderRadius: BorderRadius.circular(4.w),
        ),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(4.w),
          child: ImageNetwork(
            url: "${link.image}",
            width: 12.w,
            height: 12.w,
          ),
        ),
      ),
    );
  }

  void _onTapItem(BuildContext context, Link link) {
    final uri = Uri.tryParse("$api_base_url${link.link}");
    debugPrint("$uri");
    final mId = int.tryParse("${uri?.pathSegments?.last}");
    final r = int.tryParse("${uri?.queryParameters["r"]}");
    Navigator.of(context).pushNamed(
      ProductPage.routeName,
      arguments: ProductPageArgs(null, null, mId, r),
    );
  }
}
